// pages/Goods/EvaluationEvent/EvaluationEvent.js
//获取应用实例
const app = getApp()
const assetsAddress = app.globalData.global.assetsIp + '/' + app.globalData.global.assetsName;
import { updateGoodsEvaluationInfo } from '../../../api/goods';
import { checkFile } from '../../../utils/util';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    assetsAddress: assetsAddress,
    uploadFileUrl: app.globalData.global.uploadFileUrl,
    brandId: app.globalData.global.brandId,
    order: {},
    logisticsServices: '',// 物流服务
    descriptionConsistent: 5, // 描述
    autosize: { minHeight: 80 },
    content: '',
    fileList: [],
    isAnonym: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if(options.item){
      var item = JSON.parse(options.item);
      console.log(item)
      this.setData({
        order: item,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  // 切换物流服务评价
  selLogisticsServices(e){
    let item = e.target.dataset.item||e.currentTarget.dataset.item;
    this.setData({
      logisticsServices: item,
    })
  },
  onChange(e){
    this.setData({
      descriptionConsistent: e.detail,
    });
  },
  contChange(event) {
    // event.detail 为当前输入的值
    this.setData({
      content: event.detail,
    })
  },
  // 选择图片
  afterRead(event) {
    let _this = this;
    const { file } = event.detail;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    console.log(file,'file')
    var token = wx.getStorageSync('access_token')
    wx.uploadFile({
      url: _this.data.uploadFileUrl, // 上传接口
      filePath: file.url,
      name: 'file',
      formData: { 'brandId': _this.data.brandId ,'access_token': token,},
      success(res) {
        // 上传完成需要更新 fileList
        const { fileList = [] } = _this.data;
        // fileList.push({ ...file, url: res.data });
        console.log(res,'res')
        var res = JSON.parse(res.data);
        if(res.success){
          var types = checkFile(res.data);
          if(types == 'video'){
            fileList.push({url: res.data,isVideo: true,deletable: true})
          }else if(types == 'image'){
            fileList.push({url: res.data,isImage: true,deletable: true})
          }
          console.log(fileList,'fileList')
          _this.setData({ fileList });
        }else{
          wx.showToast({
            title: res.errMessage,
            icon: 'none',
          })
        }
      },
    });
  },
  // 提交评价
  submit(){
    let _this = this;
    const { order } = _this.data;
    var grade = 0;
    if(_this.data.descriptionConsistent < 5 && _this.data.descriptionConsistent > 1){
      grade = 1;
    }
    if(_this.data.descriptionConsistent == 1){
      grade = 2;
    }
    var imageUrlArr = []
    for (let index = 0; index < _this.data.fileList.length; index++) {
      const item = _this.data.fileList[index];
      imageUrlArr.push(item.url);
    }
    updateGoodsEvaluationInfo({orderCode: order.orderCode,goodsListsId: order.goodsInfo[0].goodsListsId,grade: grade,content: _this.data.content,descriptionConsistent: _this.data.descriptionConsistent,logisticsServices: _this.data.logisticsServices == '' ? 5 : _this.data.descriptionConsistent,serviceAttitude: 5,imageUrl: imageUrlArr.join(','),isAnonym: _this.data.isAnonym})
    .then(res => {
      if(res.success){
        wx.showToast({
          title: '评价成功',
          icon: 'success',
        })
        wx.navigateBack({
          delta: 1,
        })
      }else{
        wx.showToast({
          title: res.errMessage,
          icon: 'none',
        })
      }
    })
  },
  // 切换是否匿名
  selAnonym(){
    let _this = this;
    _this.setData({
      isAnonym: _this.data.isAnonym == '1' ? '' : '1'
    })
  },
})